//
//  SeePictureViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/5/12.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SeePictureViewController.h"
#import "ZYBrowerView.h"

#define kMaxZoomScale 2
#define kMinZoomScale 1

@interface SeePictureViewController ()<UIScrollViewDelegate>

@property (nonatomic ,strong) UIScrollView *imgScroll;
@property (nonatomic ,strong) NSMutableArray *imagArry;
@property (nonatomic, strong) UILabel * pageNumlable;
@property (nonatomic, assign) CGFloat offsetX;
@property (nonatomic, strong) UIView *shareView;
@property (nonatomic, assign) BOOL isOpen;
//@property (nonatomic, strong) UIImageView *saveImg;


@end

@implementation SeePictureViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blackColor];
    [self.view addSubview:[self imgScroll]];
    [self creatUI];
    [self creatshareView];
    // Do any additional setup after loading the view.
}
-(void)creatshareView{
    self.shareView=[[UIView alloc]initWithFrame:CGRectMake(0, MainH, MainW, 100)];
    self.shareView.backgroundColor=Gray_Color;
    [self.view addSubview:self.shareView];
    UIButton *save=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, MainW, 49)];
    save.backgroundColor=[UIColor whiteColor];
    [save setTitleColor:TextColor3 forState:UIControlStateNormal];
    [save setTitle:@"保存至相册" forState:UIControlStateNormal];
    save.titleLabel.font=FontAndStyle14;
    [save addTarget:self action:@selector(saveToimagClick) forControlEvents:UIControlEventTouchUpInside];
    [self.shareView addSubview:save];
    UIButton *share=[[UIButton alloc]initWithFrame:CGRectMake(0, 50, MainW, 50)];
    share.backgroundColor=[UIColor whiteColor];
    [share setTitleColor:TextColor3 forState:UIControlStateNormal];
    [share setTitle:@"分享" forState:UIControlStateNormal];
    share.titleLabel.font=FontAndStyle14;
    [share addTarget:self action:@selector(shareToClick) forControlEvents:UIControlEventTouchUpInside];
    [self.shareView addSubview:share];
    
}
-(void)showView{
    self.isOpen=YES;
    [UIView animateWithDuration:0.35 animations:^{
        self.shareView.frame=CGRectMake(0, MainH-100, MainW, 100);
    }];
}
-(void)disView{
    self.isOpen=NO;
    [UIView animateWithDuration:0.35 animations:^{
        self.shareView.frame=CGRectMake(0, MainH, MainW, 100);
    }];
}
-(UIScrollView *)imgScroll{
    if (!_imgScroll) {
        _imgScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, MainW, MainH-64)];
        _imgScroll.backgroundColor=[UIColor blackColor];
        _imgScroll.backgroundColor=[UIColor blackColor];
        _imgScroll.showsHorizontalScrollIndicator = NO;
        _imgScroll.showsVerticalScrollIndicator = NO;
        _imgScroll.pagingEnabled = YES;
        _imgScroll.delegate = self;
        _imgScroll.contentSize = CGSizeMake(MainW * self.imagesUrlAry.count, MainH-109);

    }
    return _imgScroll;
}
-(void)creatUI{
    UIView*navView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 64)];
    navView.backgroundColor=HJ_Color(129, 129, 129);
   // navView.alpha=0.6;
    [self.view addSubview:navView];
    UIButton *back=[[UIButton alloc]initWithFrame:CGRectMake(20, 35, 10, 16)];
    [back setBackgroundImage:[UIImage imageNamed:@"goods_back_white"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:back];
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 28, 40, 32)];
    //btn.backgroundColor=[UIColor redColor];
    [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:btn];
    UIButton *more=[[UIButton alloc]initWithFrame:CGRectMake(MainW-30, 35, 20, 16)];
    [more setTitle:@"···" forState:UIControlStateNormal];
    [more setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [more addTarget:self action:@selector(shareClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:more];
    UILabel * pageNumlable = [[UILabel alloc] initWithFrame:CGRectMake(MainW - 90, MainH-40, 80, 30)];
    pageNumlable.textColor = [UIColor whiteColor];
    pageNumlable.backgroundColor = [UIColor clearColor];
    pageNumlable.textAlignment = NSTextAlignmentCenter;
    pageNumlable.font = [UIFont systemFontOfSize:15];
    pageNumlable.layer.masksToBounds = YES;
    pageNumlable.layer.cornerRadius = 10;
    self.pageNumlable = pageNumlable;
    [self.view insertSubview:pageNumlable aboveSubview:self.imgScroll];
    for (int i=0; i<self.imagesUrlAry.count; i++) {
        UIScrollView * smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(MainW*i, 0, MainW, MainH-109)];
        smallScrollView.delegate = self;
        smallScrollView.tag = 1000;
        smallScrollView.maximumZoomScale = kMaxZoomScale;
        smallScrollView.minimumZoomScale = kMinZoomScale;
        [_imgScroll addSubview:smallScrollView];
        UIImageView *img=[[UIImageView alloc]init];
        img.contentMode = UIViewContentModeScaleAspectFit;
        [img sd_setImageWithURL:[NSURL URLWithString:self.imagesUrlAry[i]]];
        UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClicked)];
        img.userInteractionEnabled = YES;
        [img addGestureRecognizer:singleRecognizer];
        if (i==0) {
            self.saveImg=img;
        }
        [self.imgScroll addSubview:img];
    }
    
}
-(void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)tapClicked{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)twoTapedGesture:(UITapGestureRecognizer *)gesture{
    UIScrollView * scrollView = (UIScrollView *)gesture.view.superview;
    CGFloat currentScale = scrollView.zoomScale;
    
    if (currentScale > kMinZoomScale) {
        currentScale = kMinZoomScale;
        [scrollView setZoomScale:kMinZoomScale animated:YES];
    }else if (currentScale < kMinZoomScale) {
        [scrollView setZoomScale:kMaxZoomScale animated:YES];
    }else if (currentScale == kMinZoomScale){
        [scrollView setZoomScale:kMaxZoomScale animated:YES];
    }else if (currentScale == kMaxZoomScale){
        [scrollView setZoomScale:kMinZoomScale animated:YES];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self disView];
    CGFloat x = scrollView.contentOffset.x;
    if (x == self.offsetX) {
        
    }else{
        
        self.offsetX = x;
        NSInteger currentIndex = x/MainW;
        [self.saveImg sd_setImageWithURL:[NSURL URLWithString:self.imagesUrlAry[currentIndex]]];
        self.pageNumlable.text = [NSString stringWithFormat:@"%zd/%zd",(currentIndex + 1),_imagesUrlAry.count];
        //NSString * imageUrl = self.imagesUrlAry[currentIndex];
        
        //[self setImageForImageView:imageView scrollView:smallScrollView imageUrl:imageUrl];
    }

}
#pragma mark ----分享
-(void)shareToClick{
    
}
#pragma mark ----保存到相册
-(void)saveToimagClick{
    [self disView];
    UIImageWriteToSavedPhotosAlbum(self.saveImg.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
}
#pragma mark ----弹出底部视图
-(void)shareClick{
    if (self.isOpen==NO) {
        [self showView];
    }else{
        [self disView];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 保存图片后的回调
- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError:  (NSError*)error contextInfo:(id)contextInfo

{
    
    NSString*message =@"呵呵";
    
    if(!error) {
        
        message =MyString(@"成功保存到相册");
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:MyString(@"提示") message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:MyString(@"确定") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:action];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }else
        
    {
        
        message = [error description];
        
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:MyString(@"提示") message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:MyString(@"确定") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:action];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }
    
}


@end
